#ifndef __DRIVER_UART_H__
#define __DRIVER_UART_H__

#include "stm32f0xx.h"

#define Cbuf_Init(cbuf) cbuf.m_getIdx = cbuf.m_putIdx = 0
#define Cbuf_Len(cbuf) ((cbuf.m_putIdx + cbuf##_Size - cbuf.m_getIdx) & (cbuf##_Size - 1))
#define Cbuf_Push(cbuf, elem) (cbuf.m_entry)[cbuf.m_putIdx++ & (cbuf##_Size - 1)] = elem
#define Cbuf_Pop(cbuf) (cbuf.m_entry)[cbuf.m_getIdx++ & (cbuf##_Size - 1)]
#define Cbuf_IsEmpty(cbuf) (Cbuf_Len(cbuf) == 0)

#define Uart_RxBuff_Size 128

typedef struct
{
  uint16_t m_getIdx;
  uint16_t m_putIdx;
  uint8_t m_entry[128];
} uart_buff_t;

typedef enum
{
  AP_SET1,
  AP_SET1_ACK,
  AP_SET2,
  AP_SET2_ACK,
  AP_WSTA,
  AP_WSTA_ACK,
  AP_WANN,
  AP_WANN_ACK,
  AP_SOCKA,
  AP_SOCKA_ACK,
  AP_SOCKLKA,
  AP_SOCKLKA_ACK,
  AP_ENTM,
  AP_ENTM_ACK,
  AP_PAYLOAD,
} AP_SETTING;

typedef union
{
  uint8_t Bytes[93];
  struct
  {
    uint8_t Header[9];   //Header
    uint16_t Length;     // 数据长度
    uint32_t ID;         // ID
    uint32_t Counter;    //封包数量
    uint32_t Period;     //封包周期
    uint8_t Payload[64]; //Packet
    uint8_t End[6];      //End
  } __attribute__((__packed__)) Packet;
} __attribute__((__packed__)) uart_txbuff_t;

extern uart_txbuff_t Uart_TxBuff;
extern uart_buff_t Uart_RxBuff;

void ComPort_Uart_Init_Fuc(void);
void ComPort_Uart_Deinit_Fuc(void);
uint16_t Uart_Rx_GetLen(void);
uint8_t Uart_Rx_GetByte(void);
void Communication_Handler(void);                                   // 任务自己添加
void Communication_Test_Handler(void);                              // 任务自己添加
void User_Packet_Sender(uint8_t rssi, uint8_t *data, uint8_t size); // 传输固定的包
void User_Bytes_Sender(char *data);                                 // 传输字节

#endif
